package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.SetMealCheckGroupMapper;
import com.itheima.mapper.SetMealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.HealthStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description: 套餐的业务层
 * @author: wangjingliang
 * @create: 2019-11-09 21:19
 **/
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private SetMealCheckGroupMapper setMealCheckGroupMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();
        //判断参数是否存在
        if (currentPage < 1 || currentPage == null) {
            currentPage = 1;
        }
        if (pageSize < 0 || pageSize == null) {
            pageSize = 10;
        }
        if (queryString == null) {
            queryString = "";
        }
        //判断是否为null或者为" "
        if (HealthStringUtils.isEmpty(queryString)) {

            queryString = queryString.trim();
        }
        //基于mybatis的分页插件
        PageHelper.startPage(currentPage, pageSize);
        Page<Setmeal> page = setMealMapper.findPage("%" + queryString + "%");

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 对套餐的数据添加
     *
     * @param: SetMeal
     * @return: int
     * @date: 2019/11/8
     */
    @Override
    public int add(Integer[] checkgroupIds, Setmeal setmeal) {
//添加时检查项与检查表的关联关系,必须对中间表进行添加
        String code = setmeal.getCode();
        String name = setmeal.getName();
        String helpCode = setmeal.getHelpCode();

        //判断编码和名称是否已经存在
        int byCode = setMealMapper.findByCode(code);
        if (byCode > 0) {
            throw new RuntimeException("该编码已存在");
        }
        int byName = setMealMapper.findByName(name);
        if (byName > 0) {
            throw new RuntimeException("请该名称已存在");
        }
        int ByhelpCode = setMealMapper.findByHelpCode(helpCode);
        if (ByhelpCode > 0) {
            throw new RuntimeException("请该助记已存在");
        }
        //再插入套餐
        int add = setMealMapper.add(setmeal);
        Integer id = setmeal.getId();
        if (id == null) {
            throw new RuntimeException(MessageConstant.EXCEPION);
        }
        redisTemplate.boundListOps("setmealList").rightPush(setmeal);

        //先插入中间表
        int addSetmealAndCheckitemIds = 0;

        for (int checkgroupId : checkgroupIds) {

            addSetmealAndCheckitemIds = setMealCheckGroupMapper.addSetmealAndCheckitemIds(checkgroupId, id);
        }
        if (addSetmealAndCheckitemIds == 0 && checkgroupIds.length > 0) {
            throw new RuntimeException(MessageConstant.EXCEPION);
        }

        return add;
    }

    @Override
    public List<Setmeal> findAll() {
        List<Setmeal> setmealList = null;

        setmealList = setMealMapper.selectAll();
        if (setmealList == null || setmealList.size() == 0) {
            throw new RuntimeException(MessageConstant.EXCEPION);
        }
        return setmealList;
    }

    /**
     * 查询套餐数据及其对应的检查项和检查组
     *
     * @param: a
     * @return: null
     * @date: 2019/11/18
     */
    @Override
    public Setmeal findById(Integer id) {
        if (id == null) {
            throw new RuntimeException(MessageConstant.EXCEPION);
        }
        Setmeal setmeal = setMealMapper.findById(id);
        if (setmeal == null) {
            throw new RuntimeException(MessageConstant.EXCEPION);
        }
        return setmeal;
    }

    /**
     * 根据id查询套餐数据
     *
     * @param: a
     * @return: null
     * @date: 2019/11/18
     */

    @Override
    public Setmeal findsetmealById(Integer id) {
        if (id == null) {
            throw new RuntimeException(MessageConstant.EXCEPION);
        }
        Setmeal setmeal = null;
        //从redis中调数据
        List<Setmeal> setmealList = redisTemplate.boundListOps("setmealList").range(0, -1);
        for (Setmeal setmeal1 : setmealList) {
            if (setmeal1.getId().equals(id)) {
                setmeal = setmeal1;
            }
        }
        return setmeal;

    }
}
